<#assign tit>私教礼包销售</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<script type="text/javascript">
  function seldj() {
    var fm = getEl("fm_input");
    var url = "getEarnestList?type=2";
    if(fm.memberCode.value=="") {
      window.alert("请先输入会员卡号!");
      return;
    }
    url += "&memberCode="+fm.memberCode.value;
    openWin(url, 620, 400);
  }
  function seldjOk(vv) {
    var ss = vv.split(",");
    var fm = getEl("fm_input");
    fm.earnestId.value = ss[0];
    fm.earnestAmount.value = ss[1];
    chgfact();
  }

  function getminfo(c) {
    if(c!="") {
      getJson("getMember?cardCode="+c, function(data){
        if(data.ret) {
          var mb = data.data;
          var fm = getEl("fm_input");
          fm.cardCode.value = mb.card;
          fm.memberCode.value = mb.code;
          fm.memberName.value = mb.name;
          fm.contractTime.value = mb.contractEnd;
          if(mb.ptContractEnd && mb.ptContractEnd>=getDateStr(new Date())) {
            var dt = parseDate(mb.ptContractEnd);
            dt.addDay(1);
            fm.beginTime.value = getDateStr(dt);
          } else
            fm.beginTime.value = getDateStr(new Date());
          setpdt();
        } else {
          window.alert(data.msg);
          cldc();
        }
      });
    } else {
      cldc();
    }
  }

  function cldc() {
    var fm = getEl("fm_input");
    fm.cardCode.value = "";
    fm.memberCode.value = "";
    fm.memberName.value = "";
    fm.contractTime.value = "";
    fm.beginTime.value = "";
    fm.endTime.value = "";
  }

  var _pkg = null;
  function chgpkg() {
    var fm = getEl("fm_input");
    var pc = fm.packageCode.value;
    if(pc!="") {
      var url = "getPackageItem?code="+pc;
      getJson(url, function(data){
        if(data.ret) {
          setpkg(data.data);
        } else {
          window.alert(data.msg);
          setpkg(null);
        }
      });
    } else {
      setpkg(null);
    }
  }

  function setpkg(pkg) {
    _pkg = pkg;
    var fm = getEl("fm_input");
    if(pkg) {
      fm.ptName.value = pkg.ptCode+" - "+pkg.ptName;
      fm.quantity.value = pkg.ptQuantity;
      fm.factPrice.value = formatMoney(pkg.totalPrice);
      setProdList(pkg.itemList);
    } else {
      fm.ptName.value = "";
      fm.factPrice.value = "";
      fm.factPrice.value = "";
      setProdList(null);
    }
    setpdt();
    chgfact();
  }
  function setProdList(ls) {
    var i, arr = [];
    arr.push('<table class="list">');
    for(i=0; ls && i<ls.length; i++) {
      arr.push('<tr class="line'+((i%2)+1)+'">'
          + '<td width="60%" class="left">'+ls[i].pcode+'-'+ls[i].pname+'</td>'
          + '<td width="20%">'+ls[i].pquantity+'</td>'
          + '<td width="20%"></td>'
          + '</tr>')
    }
    arr.push('</table>');
    var div = getEl("div_prodlist");
    if(div) div.innerHTML = arr.join("");
  }

  function setpdt() {
    var fm = getEl("fm_input");
    if(_pkg) {
      var dt = parseDate(fm.beginTime.value);
      if(dt) {
        dt.addDay(_pkg.ptValidDays);
        fm.endTime.value = getDateStr(dt);
      }
    } else {
      fm.endTime.value = "";
    }
  }

  function chgfact() {
    var fm = getEl("fm_input");
    if(fm.factPrice.value=="") {
      fm.payAmount.value = "";
      return;
    }
    var m1 = parseFloat(fm.factPrice.value);
    if(isNaN(m1)) {
      window.alert("金额错误");
      return;
    }
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    fm.factPrice.value = formatMoney(m1);
    fm.payAmount.value = formatMoney(m1-m2);
  }

  function save(fm) {
    if(!fm) fm = getEl("fm_input");
    if(fm.memberCode.value=="") {
      window.alert("请输入会员卡号");
      return false;
    }

    if(_pkg==null) {
      window.alert("请选择私教礼包");
      return false;
    }

    var mm = fm.payAmount.value;
    post(fm, function(){
      openWin("getPay?money="+mm, 500, 400);
      return false;
    });
    return false;
  }
  function payOk(vv) {
    var fm = getEl("fm_input");
    fm.payls.value = vv;
    postForm(fm.action, fm, saveRet);
  }
  function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) {
      getEl("fm_input").reset();
      setProdList(null);
      //window.location = "print?id="+data.data;
    }
  }
</script>
<div style="width:600px;">

  <form id="fm_input" method="post" action="_add" onsubmit="return save(this);">
    <input type="hidden" name="payls" value="">
    <div class="kbox">
      <table class="input w4">
        <tr>
          <th width="85">会员卡号</th>
          <td width="160"><input type="text" name="cardCode" value="" onchange="getminfo(this.value);" maxlength="20"></td>
          <th width="85">会员编号</th>
          <td width="160"><input type="text" name="memberCode" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>会员姓名</th>
          <td><input type="text" name="memberName" value="" maxlength="10" class="disable" readonly></td>
          <th>合同终止日</th>
          <td><input type="text" name="contractTime" value="" maxlength="10" class="disable" readonly></td>
        </tr>
        <tr>
          <th>定金编号</th>
          <td><input type="text" name="earnestId" value="" style="width:90px;" class="disable" readonly>
            <input type="button" class="opwin" value="..." onclick="seldj();"/></td>
          <th>定金金额</th>
          <td><input type="text" name="earnestAmount" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>私教礼包</th>
          <td colspan="3"><select name="packageCode" onchange="chgpkg();" class="w2" req="私教礼包"><option value="">--请选择--</option><#if pkgList??><#list pkgList as t><option value="${t.code!}">${t.code!} - ${t.chName!}</option></#list></#if></select></td>
        </tr>
        <tr>
          <th>私教课程</th>
          <td colspan="3"><input type="text" name="ptName" value="" class="w2 disable" readonly></td>
        </tr>
        <tr>
          <th>上课教练</th>
          <td><select name="coachCode" req="上课教练"><option value="">--请选择--</option><#if coachList??><#list coachList as t><option value="${t.code!}">${t.code!} - ${t.chName!}</option></#list></#if></select></td>
          <th>课程数量</th>
          <td><input type="text" name="quantity" value=""  maxlength="6" class="disable" readonly></td>
        </tr>

        <tr>
          <th>开始日期</th>
          <td><input type="text" name="beginTime" value="" class="date disable" maxlength="10" readonly onchange="chgdt1();" onfocus="if(!this.readOnly)cal(this);"></td>
          <th>结束日期</th>
          <td><input type="text" name="endTime" value="" class="date disable" maxlength="10" readonly></td>
        </tr>
        <tr>
          <th>实际售价</th>
          <td><input type="text" name="factPrice" value="" req="实际售价" class="disable" readonly></td>
          <th>应收金额</th>
          <td><input type="text" name="payAmount" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>备注</th>
          <td colspan="3"><textarea name="note"></textarea></td>
        </tr>
      </table>

      <div style="margin:0 20px;">
        <div class="lsinfo">礼包包含商品</div>
        <table class="list">
          <tr>
            <th width="60%" class="left">名称</th>
            <th width="20%">数量</th>
            <th width="20%">库存</th>
          </tr>
        </table>
        <div id="div_prodlist"></div>
      </div>

    </div>
    <div class="button"><input type="button" value="确认并提交" onclick="save();"></div>
  </form>
</div>

</@page.html>